/**
 * 
 */
package ar.edu.utn.frsf.cidisi.ginet.generation.upcolbpip2cginet.elementstemplate;

import org.eclipse.emf.common.util.EList;
import org.pnml.tools.epnk.pnmlcoremodel.AnnotationGraphics;
import org.pnml.tools.epnk.pnmlcoremodel.Coordinate;
import org.pnml.tools.epnk.pnmlcoremodel.Name;
import org.pnml.tools.epnk.pnmlcoremodel.PnmlcoremodelFactory;
import org.pnml.tools.epnk.pntypes.hlpng.pntd.hlpngdefinition.Declaration;

import ar.edu.utn.frsf.cidisi.ginet.abstractginet.Arc;
import ar.edu.utn.frsf.cidisi.ginet.abstractginet.Page;
import ar.edu.utn.frsf.cidisi.ginet.abstractginet.Place;
import ar.edu.utn.frsf.cidisi.ginet.abstractginet.RefPlace;
import ar.edu.utn.frsf.cidisi.ginet.abstractginet.Transition;
import ar.edu.utn.frsf.cidisi.upcolbpip.interactionprotocols.BusinessMessage;
import ar.edu.utn.frsf.cidisi.upcolbpip.interactionprotocols.ControlFlowSegment;
import ar.edu.utn.frsf.cidisi.upcolbpip.interactionprotocols.InteractionPath;
import ar.edu.utn.frsf.cidisi.upcolbpip.interactionprotocols.InteractionProtocolElement;

/**
 * @author Neri
 *
 */
public class ExceptionUpcolbpipStructure extends ChoreographyStructure {
	
	public ExceptionUpcolbpipStructure(Integer i){		
		super();
		this.pos = i;
		//Name		
		AnnotationGraphics graphicsName = PnmlcoremodelFactory.eINSTANCE
				.createAnnotationGraphics();
		
		Name nameLabel = PnmlcoremodelFactory.eINSTANCE.createName();
		
		Coordinate coordinate2 = PnmlcoremodelFactory.eINSTANCE.createCoordinate();
		
		coordinate2.setX(-9);
		coordinate2.setY( -39);
		
		graphicsName.setOffset(coordinate2);
		
		nameLabel.setText("Exception");
		nameLabel.setGraphics(graphicsName);
		
		this.abstractPage.setName(nameLabel);
	}
	
	@Override
	public void createPageStructure(InteractionProtocolElement element, Place placeIn, Place placeOut, EList<Declaration> ginetDeclarations, Place termination, EList<BusinessMessage> messages){		
		
		this.fillVarDeclAndNameSort(ginetDeclarations);
		
		this.setPageName(element);
		
		EList<InteractionPath> interP = ((ControlFlowSegment)element).getInteractionPath(); 
		
		RefPlace refPlaceIn = this.createRefPlace("InPlace",  1, 1, "InPlace", -4, -4, "1", placeIn);
		
		RefPlace refPlaceOut = this.createRefPlace("OutPlace", 7, 1, "OutPlace", -4, -4, "1", placeOut);
		
		Transition transitionIn = this.createTransition("transitionIn", 2, 1, "transitionIn", -4, -9, "1", "1", null);
		
		Place placeInException = createPlace("placeInException", 3, 1, "placeInException", -4, -4, this.getVarDec("gi"), 6, 21, true, "1");
		
		Transition transitionOut = this.createTransition("transitionOut", 6, 1, "transitionOut", -4, -9, "1", "1", null);
		
		Place placeOutException = createPlace("placeOutException", 5, 1, "placeOutException", -4, -4, this.getVarDec("gi"), 6, 21, true, "1");
		
		Arc arcIn = this.createArc("", this.getVarDec("gi"), -37, -3, refPlaceIn, transitionIn);
		this.abstractPage.getObject().add(arcIn);
		
		Arc arcIn2 = this.createArc("", this.getVarDec("gi"), -37, -3, transitionIn, placeInException);
		
		Arc arcOut = this.createArc("", this.getVarDec("gi"), -37, -3, transitionOut, refPlaceOut);
		this.abstractPage.getObject().add(arcOut);
		
		Arc arcOut2 = this.createArc("", this.getVarDec("gi"), -37, -3, placeOutException, transitionOut);
		
		this.abstractPage.getObject().add(arcIn2);
		this.abstractPage.getObject().add(transitionIn);
		this.abstractPage.getObject().add(placeInException);
		this.abstractPage.getObject().add(arcOut2);
		this.abstractPage.getObject().add(transitionOut);
		this.abstractPage.getObject().add(placeOutException);
		
		if(interP.size() > 0){
			Page page = createPage("page", 4, 1, "page", -9, -39);
			
			this.createInteractionProtocol(interP.get(0), 0, page, placeInException, placeOutException, ginetDeclarations, termination, messages);
			
			this.abstractPage.getObject().add(page);
		}
		
		if(interP.size() > 1){
			Transition transitionTrigger = this.createTransition("trigger", 2, 2, "trigger", -9, -39, "1", "1", null);
			this.abstractPage.getObject().add(transitionTrigger);
			
			Place placeCatchIn = createPlace("cacthIn", 3, 2, "cacthIn", -4, -4, this.getVarDec("gi"), 6, 21, false, "1");
			this.abstractPage.getObject().add(placeCatchIn);
			
			Arc arcPInTTr = this.createArc("", this.getVarDec("gi"), -19, -6, refPlaceIn, transitionTrigger);
			
			Arc arcTTrPCIn = this.createArc("", this.getVarDec("gi"), -9, -9, transitionTrigger, placeCatchIn);
			this.abstractPage.getObject().add(arcPInTTr);
			this.abstractPage.getObject().add(arcTTrPCIn);
			
			Place placeCatchOut = this.createPlace("cacthOut", 5, 2, "cacthOut", -4, -4,this.getVarDec("gi"), 6, 21, false, "1");
			this.abstractPage.getObject().add(placeCatchOut);
			
			Transition transitionExcepOt = this.createTransition("exceptionot", 6, 2, "exceptionot", -9, -39, "1", "1", null);
			this.abstractPage.getObject().add(transitionExcepOt);
			
			Arc arcPCOutTExot = this.createArc("", this.getVarDec("gi"), -9, -9, placeCatchOut, transitionExcepOt);
			this.abstractPage.getObject().add(arcPCOutTExot);
			
			Arc arcTExOtPOut = this.createArc("", this.getVarDec("gi"), -19, -6, transitionExcepOt, refPlaceOut);
			
			this.abstractPage.getObject().add(arcTExOtPOut);
						
			for(int i = 1; i < interP.size(); i++){
				Page page = createPage("page" + i, 4, i+1, "page" + i, -9, -39);
				
				this.createInteractionProtocol(interP.get(i), i, page, placeCatchIn, placeCatchOut, ginetDeclarations, termination, messages);
				
				this.abstractPage.getObject().add(page);
			}
		}
		
		this.abstractPage.getObject().add(refPlaceIn);
		this.abstractPage.getObject().add(refPlaceOut);
	}
}